<?php

/*
 * 团队设置
 *
 * */
namespace App\Http\Controllers\Admin;



use App\Common\CommonHelpers;
use App\Http\Controllers\Controller;
use App\Libraries\Common\CommonFun;
use App\Models\ProjectMonitorTeam;
use App\Models\RsReport;
use App\Models\RsReportAuth;
use App\Models\RsTeam;
use App\Models\RsTeamUser;
use App\Models\RsUsers;
use App\Models\User;
use Illuminate\Http\Request;

class TeamController extends Controller{


    //团队设置列表
    public function index(Request $request){
        return view('admin.team.index');
    }

    //团队设置api
    public function apiList(Request $request){
        //查询消息数据
        $data=RsTeam::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }

    //获取创建信息
    public function create(Request $request){
        $model=[];
        //获取权限人员
        $data=RsUsers::lists();
        if($data){
            $datas=$data['data'];
        }else{
            $datas=[];
        }
        //获取团队信息
        $team_list=ProjectMonitorTeam::lists();
        if($team_list){
            $team_lists=$team_list['data'];
        }else{
            $team_lists=[];
        }

        if($id=$request->input("id",false)){
            $model=RsTeam::where(['id'=>$id])->first();
        }
        return view('admin.team.create',['model'=>$model,'auth'=>[],'data'=>$datas,'team_list'=>$team_lists]);
    }

    //获取团队人员信息
    public function teamList(Request $request){
        $team_id=$request->input("team_id",'');
        $data=RsTeamUser::with('getRsUser','getUser')->where(['team_id'=>$team_id])->orWhere(['team_id'=>null])->get()->toArray();
        $count=count($data);
        $data=compact('count', 'data');

        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);

    }

    //添加人员
    public function auth(Request $request){
        $id=$request->input("id",false);

        $data=RsUsers::lists();
        if($id){
            $model=RsTeamUser::query()->where(['id'=>$id])->first()->toArray();
        }else{
            $model=[];
        }
        return view('admin.team.selectlist',['data'=>$data['data'],'model'=>$model]);
    }


    //添加数据操作
    public function teamCreateApi(Request $request){
        $data=[];
        $ex=explode('_',$request->input('name'));
        $data['user_id']=$ex[0];
        $data['username']=$ex[1];
        $data['remark']=$request->input('remark');
        $data['id']=$request->input("id",false);

        $res=RsTeamUser::creates($data);
        if($res){
            return CommonHelpers::responseFormat(0,'','添加成功');

        }else{
            return CommonHelpers::responseFormat(0,'','添加失败');

        }
    }

    //删除添加的人员
    public function authDelete(Request $request){
        $id=$request->input("id");
        $data=RsTeamUser::where(['id'=>$id])->delete();
        if($data){
            return CommonHelpers::responseFormat(0,'','删除成功');

        }else{
            return CommonHelpers::responseFormat(1,'','删除失败');

        }
    }

    //添加团队数据
    public function createApi(Request $request){
        $data=[];
        $messages = [
            'name.required' => '请选择负责人',
            'monitor_name.required'=>'请选择团队信息'
            // 'shop_id.required' => '缺少店铺信息'
        ];
        $validate = \Validator::make($request->all(), [
            'name'=>'required|string',   //消息序号
            'monitor_name'=>'required|string',   //消息序号

        ],$messages);
        //判断是否参数完整
        if ($validate->fails()){

          return CommonHelpers::responseFormat(100,'',$validate->errors()->first());

           // return $this->failed($validate->errors()->first());

        }


        $ex=explode('-',$request->input('name'));
        $data['team_name']=$request->input("team_name");
        $data['id']=$request->input("id",false);
        $data['team_person']=$ex[1];
        $data['team_person_id']=$ex[0];
        $data['team_no']=CommonFun::createTradeId('team');
        $data['monitor_team_uid']=$request->input("monitor_team_uid");

        $res=RsTeam::created($data);
        if($res){
            return CommonHelpers::responseFormat(0,'','添加成功');

        }else{
            return CommonHelpers::responseFormat(100,'','添加失败');

        }
    }

    //删除团队信息
    public function delete(Request $request){
        $id=$request->input("id");
        try {
            //删除权限人员
            RsTeamUser::where(['team_id'=>$id])->delete();
            //删除团队
            RsTeam::where(['id'=>$id])->delete();

            return CommonHelpers::responseFormat(0,'','删除成功');

        }catch (\Exception $exception){
            return CommonHelpers::responseFormat(1,'','删除失败');

        }
    }

    //删除无用团队信息
    public function userDel(){
        $data=RsTeamUser::where(['team_id'=>null])->orWhere(['team_id'=>''])->delete();
        return CommonHelpers::responseFormat(0,'','');

    }


}
